$(document).ready(function(){
	var checkedAll=$(".check-all");
	var checkedOne=$(".check-one");
	var rows =$("#tbody tr");
	var subAll=$("#priceTotal");
	var countAll=$("#selectedTotal");
	var delAll=$("#deleteAll");
	var count="";
	var del="";
	var price="";
	var sub="";
	var tbr=[
		["images/1.jpg","Casio/卡西欧 EX-TR350","5999.88","1","5999.88"],
		["images/2.jpg","Canon/佳能 PowerShot SX50 HS","3888.50","1","3888.50"],
		["images/3.jpg","Sony/索尼 DSC-WX300","1428.50","1","1428.50"],
		["images/4.jpg","Fujifilm/富士 instax mini 25","640.60","1","640.60"]
		]
	//全选
	 $(".check-all").on('change', function() {
		if (this.checked) {
			for(var i=0;i<checkedOne.length;i++){
				checkedOne[i].checked="checked";
				checkedAll[0].checked="checked";
				checkedAll[1].checked="checked";
			}
			subAllF();
		} else {
		   for(var i=0;i<checkedOne.length;i++){
			checkedOne[i].checked="";
			checkedAll[0].checked="";
			checkedAll[1].checked="";
		   }
		   subAllF();
		}
		
	}); 

	//总计
	function subAllF(){
			rows = $("#tbody tr");
			var subAlls=0;
			var countAlls=0;
			rows.each(function(i,e){
				var sub=$(this).find(".subtotal").text();
				var count=$(this).find(".count-input").val();
				if(checkedOne[i].checked || checkedAll[0].checked || checkedAll[1].checked){
					subAlls=(parseFloat(subAlls)+parseFloat(sub)).toFixed(2);
					countAlls=Number(countAlls)+Number(count);
				}	
			});
			subAll.html(parseFloat(subAlls));
			countAll.html(Number(countAlls));
		}
	//获取表单信息
	rows.each(function(i,el){
		//添加数据
		$(this).find("img").attr("src",tbr[i][0]);
		$(this).find(".product").text(tbr[i][1]);
		$(this).find(".price").text(tbr[i][2]);
		$(this).find(".count-input").val(tbr[i][3]);
		$(this).find(".subtotal").text(tbr[i][4]);
		$(this).click(function(e){
			 //count=$(".count-input");
			 count=$(this).find(".count-input");
			 del=$(this).find(".reduce");
			 price=$(this).find(".price");
			 sub=$(this).find(".subtotal"); 
			//增减数量，删除
			if($(e.target).attr("class")=="add"){
				del.html("-");
				count.val(Number(count.val())+1);
				sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
			}else if($(e.target).attr("class")=="reduce"){
				if(count.val()>1){
					count.val(count.val()-1);
				}else{
					count.val(0);
					del.html("");
				}
				sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
			}else if($(e.target).attr("class")=="check-one check"){
				checkedAll[0].checked="";
				checkedAll[1].checked="";
			}else if($(e.target).attr("class")=="delete"){
				var del=confirm("确定要删除？");
				if(del){
					this.remove();
				}
			
			}
			subAllF();
		
		});
		//键盘选取
		$(this).find(".count-input").on("keyup",function(e){
			count=$(this).find(".count-input");
			del=$(this).find(".reduce");
			price=$(this).find(".price");
			sub=$(this).find(".subtotal"); 
			sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
			
			if(Number(count.val())>1){
				del.html("-");
			}else{
				del.html("");
			}
			subAllF();
		});
	});
	//全部删除
	delAll.click(function(){
		if(checkedAll[0].checked || checkedAll[1].checked){
			$("#tbody").html("");
		}
	}); 
});

	